package org.zjt.servlet3;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.concurrent.atomic.AtomicLong;

/**
 * DESC
 *
 * @author
 * @create 2017-07-17 下午4:55
 **/
@WebListener
public class MyServletSessionListener implements HttpSessionListener {

    //ONLINE_PERSON_NUM
    private static final AtomicLong ONLINE_PERSON_NUM = new AtomicLong(0);

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("创建session userid：" + httpSessionEvent.getSession().getAttribute("userId") + "\t在线人数："+ONLINE_PERSON_NUM.incrementAndGet());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("删除 session userid：" + httpSessionEvent.getSession().getAttribute("userId")+ "\t在线人数："+ONLINE_PERSON_NUM.decrementAndGet());
    }
}
